
package com.liao.badminton.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.liao.badminton.code.*;
import com.liao.badminton.dto.ActivityRecordDto;
import com.liao.badminton.service.impl.ActivityRecordServiceImpl;
import com.liao.badminton.vo.ActivityParticipatorVo;
import com.liao.badminton.vo.ActivityRecordVo;
import com.github.pagehelper.PageInfo;
import com.liao.badminton.service.ActivityRecordService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import com.liao.badminton.code.validation.Delete;
import com.liao.badminton.code.validation.Insert;
import com.liao.badminton.code.validation.Update;
import com.liao.badminton.code.validation.FindDetail;
import org.springframework.validation.annotation.Validated;

import java.util.*;

/**
 * @author liaocheng
 * @date 2024-11-15
 * description:群发布活动记录表
 */
@RestController
@RequestMapping(value = "/activityRecord")
//@PreAuthorize("@ss.hasPermission('activityRecord')")//权限管理;
public class ActivityRecordController {

    //  private static  Logger logger= LoggerFactory.getLogger(ActivityRecordController.class);

    @Autowired
    private ActivityRecordService activityRecordService;

    /**
     * 新增activityRecord
     */
    @RequestMapping(value = "/add")
    @PreAuthorize("@ss.hasPermission('activityRecord:add')")//权限管理;
    public ResponseRest<ActivityRecordVo> add(@RequestBody @Validated(Insert.class) ActivityRecordDto activityRecord) throws ProjectException {
        return activityRecordService.saveActivityRecord(activityRecord);
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/deletes")
    @PreAuthorize("@ss.hasPermission('activityRecord:deletes')")//权限管理;
    public ResponseRest<Boolean> deletes(@RequestBody @Validated(Delete.class) ActivityRecordDto param) throws ProjectException {
        return activityRecordService.deletesActivityRecord(param);
    }

    /**
     * 修改activityRecord
     */
    @RequestMapping(value = "/update")
    @PreAuthorize("@ss.hasPermission('activityRecord:update')")//权限管理;
    public ResponseRest<Boolean> update(@RequestBody @Validated(Update.class) ActivityRecordDto param) throws ProjectException {
        return activityRecordService.updateActivityRecord(param);
    }

    @RequestMapping(value = "/findDetail")
    @PreAuthorize("@ss.hasPermission('ActivityRecord:findDetail')")//权限管理;
    public ResponseRest<ActivityRecordVo> findDetail(@RequestBody @Validated(FindDetail.class) ActivityRecordDto param) throws ProjectException {
        return activityRecordService.findDetail(param);
    }

    /**
     * 分页查询列表
     */
    @RequestMapping(value = "/list")
    @PreAuthorize("@ss.hasPermission('activityRecord:list')")//权限管理;
    public ResponseRest<PageInfo<ActivityRecordVo>> list(@RequestBody PageParam<ActivityRecordDto> body) throws ProjectException {
        return activityRecordService.findActivityRecordByPageParam(body);
    }


    /**
     * 解析文本
     */
    @RequestMapping(value = "/parseTxt")
    @PreAuthorize("@ss.hasPermission('activityRecord:parseTxt')")//权限管理;
    public ResponseRest<List<ActivityParticipatorVo>> parseTxt(@RequestParam("parseTxt") String parseTxt) throws ProjectException {
        Map<String, Integer> nameSexMap = new LinkedHashMap<>();
        Map<String, String> nameRecordMap = new HashMap<>();
        List<String> nameList = new ArrayList<>();
        activityRecordService.parseTxt(parseTxt, nameSexMap, nameRecordMap, nameList);
        List<ActivityParticipatorVo> participatorVoList = CollectionUtil.newArrayList();
        nameList.forEach(name -> {
            ActivityParticipatorVo participatorVo = new ActivityParticipatorVo();
            participatorVo.setSortNum(nameList.indexOf(name) + 1);
            participatorVo.setUserName(name);
            participatorVo.setSex(nameSexMap.get(name.toLowerCase()));
            participatorVo.setTxtContent(nameRecordMap.get(name.toLowerCase()));
            participatorVoList.add(participatorVo);
        });
        Collections.sort(participatorVoList, Comparator.comparing(ActivityParticipatorVo::getSortNum));
        return ResponseRest.suc("parseTxt", participatorVoList);
    }

}
